import 'package:avatar_glow/avatar_glow.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:learning/IconFontIcons.dart';
import 'package:learning/components/MyText.dart';
import 'package:learning/generated/l10n.dart';

/// 具备展开收起功能的气泡背景文字面板
/// 气泡：背景色为Color(0xFFF8F8F8)的灰色Container
///      右上角为不规则小三角
///
/// 布局规则：
///     组件的背景是气泡背景
///     包装了[BrnExpandableText]组件，具备了展开收起的能力
///
/// ```dart
///   BrnBubbleText(
///      text: '在文本的右下角有更多或者收起按钮',
///   )
///
///   BrnBubbleText(
///      text: '具备展开收起功能的文字面板，在文本的右下角有更多或者收起按钮',
///      maxLines: 2,
///   )
///
/// ```
///
/// 相关文本组件如下:
///  * [BrnExpandableText], 气泡背景的展开收起文本组件
///  * [BrnInsertInfo], 气泡背景的文本组件
///
class SubjectCard extends StatefulWidget {
  /// 显示的文本
  final String? title;
  final int? subject;

  /// create BrnBubbleText
  const SubjectCard({Key? key, this.title, this.subject}) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _SubjectCardState createState() => _SubjectCardState();
}

class _SubjectCardState extends State<SubjectCard> {
  String? parentTitle;
  int? subject;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    subject = widget.subject;
    parentTitle = widget.title;
  }

  bool _animate = true;

  @override
  Widget build(BuildContext context) {
    List<SmallSelectedBean> listExam = [
      SmallSelectedBean(
          title: S.of(context).chengji,
          icon: IconFontIcons.iconPaihang,
          color: Colors.orangeAccent),
      SmallSelectedBean(
          title: S.of(context).cuowu,
          icon: IconFontIcons.iconHuo,
          color: Colors.redAccent),
      SmallSelectedBean(
          title: S.of(context).jingzheng,
          icon: IconFontIcons.iconYangshi_icon_tongyong_badge,
          color: Colors.redAccent),
      SmallSelectedBean(
          title: S.of(context).shoucang,
          icon: IconFontIcons.iconShoucang,
          color: Colors.blueAccent)
    ];
    List<SmallSelectedBean> listLianxi = [
      SmallSelectedBean(
          type: 2,
          title: S.of(context).leibie,
          icon: IconFontIcons.iconCategory,
          color: Colors.blueAccent,
          url: "/maincategory"),
      SmallSelectedBean(
          type: 4,
          title: S.of(context).cuowu,
          icon: IconFontIcons.iconHuo,
          color: Colors.redAccent),
      SmallSelectedBean(
          type: 3,
          title: S.of(context).leixing,
          icon: IconFontIcons.iconType1,
          color: Colors.redAccent),
      SmallSelectedBean(
          title: S.of(context).shoucang,
          icon: IconFontIcons.iconShoucang,
          color: Colors.orangeAccent)
    ];
    //icon-Category
    return Container(
        color: Colors.black.withAlpha(12),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: ListView(
          children: [
            bigSelectedWidget(
                S.current.lianxi, 1, Colors.blueAccent, listLianxi),
            smallTestSelectedWidget(),
            bigSelectedWidget(S.current.kaoshi, 0, Colors.greenAccent, []),
            smallSelectedOpenSubject(context)
          ],
        ));
  }

  @override
  void didUpdateWidget(SubjectCard oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  final ScrollController _control = new ScrollController();

  Widget bigSelectedWidget(
      title, type, Color color, List<SmallSelectedBean> list) {
    List<Widget> listWidget = [];
    if (list.length > 0) {
      listWidget.add(Expanded(
          child: Column(children: [
            smallSelectedWidget(list.elementAt(0)),
            smallSelectedWidget(list.elementAt(1))
          ]),
          flex: 1));
    }
    listWidget.add(Expanded(
        flex: 2,
        child: InkWell(
            onTap: () {
              toPushExam(title, type);
            },
            child: Container(
                alignment: Alignment.center,
                child: AvatarGlow(
                    glowCount: 1,
                    animate: _animate,
                    glowColor: color,
                    child: CircleAvatar(
                      backgroundColor: color,
                      radius: 50,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          MyText(title),
                          MyText("${parentTitle}",
                              style: TextStyle(
                                  color: Colors.white54, fontSize: 12))
                        ],
                      ),
                    ))))));
    if (list.length > 0) {
      listWidget.add(Expanded(
          child: Column(children: [
            smallSelectedWidget(list.elementAt(2)),
            smallSelectedWidget(list.elementAt(3))
          ]),
          flex: 1));
    }
    return Container(
      margin: const EdgeInsets.all(10),
      padding: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        color: Colors.white,
      ),
      width: MediaQuery.of(context).size.width,
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: listWidget,
      ),
    );
  }

  Widget smallSelectedWidget(SmallSelectedBean smallSelectedBean) {
    return InkWell(
        onTap: () {
          toPush(smallSelectedBean.title, smallSelectedBean.type,
              smallSelectedBean.url);
        },
        child: Container(
          margin: const EdgeInsets.all(10),
          padding: const EdgeInsets.all(10),
          alignment: Alignment.center,
          // color: Colors.red,
          child: Column(
            children: [
              Icon(
                smallSelectedBean.icon,
                size: 30,
                color: smallSelectedBean.color,
              ),
              MyText(
                smallSelectedBean.title,
                style: TextStyle(color: Colors.black, fontSize: 12),
              ),
            ],
          ),
        ));
  }

  Widget smallSelecteTestWidget(SmallSelectedBean smallSelectedBean) {
    return Container(
      alignment: Alignment.center,
      // color: Colors.red,
      child: Column(
        children: [
          Expanded(
              child: Icon(
            smallSelectedBean.icon,
            size: 30,
            color: smallSelectedBean.color,
          )),
          Expanded(
              child: Container(
            child: MyText(
              smallSelectedBean.title,
              style: TextStyle(color: Colors.black, fontSize: 12),
            ),
          )),
        ],
      ),
    );
  }

  Widget smallTestSelectedWidget() {
    return GridView(
        padding: EdgeInsets.all(5.0),
        primary: false,
        shrinkWrap: true,
        controller: _control,
        physics: BouncingScrollPhysics(),
        // 回弹效果
        cacheExtent: 200.0,
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 150.0,
            mainAxisSpacing: 10.0,
            crossAxisSpacing: 5.0,
            childAspectRatio: 3.0 / 2.0),
        children: getMoreGridContainer());
  }

  List<Widget> getMoreGridContainer() {
    List<SmallSelectedBean> listExam = [
      SmallSelectedBean(
          type: 5,
          title: S.of(context).beitimoshi,
          icon: IconFontIcons.iconBook_fill,
          color: Colors.greenAccent),
      SmallSelectedBean(
          type: 6,
          title: S.of(context).gongkenanti,
          icon: IconFontIcons.iconNantibanli,
          color: Colors.orangeAccent),
      SmallSelectedBean(
          type: 7,
          title: S.of(context).suijilianxi,
          icon: IconFontIcons.iconSuijihuifu16,
          color: Colors.redAccent),
      SmallSelectedBean(
          type: 8,
          title: S.of(context).duoxuanti,
          icon: IconFontIcons.iconIcon_duoxuanti,
          color: Colors.blueAccent),
      SmallSelectedBean(
          type: 9,
          title: S.of(context).danxuanti,
          icon: IconFontIcons.iconDanxuanti,
          color: Colors.orangeAccent),
      SmallSelectedBean(
          type: 10,
          title: S.of(context).panduanti,
          icon: IconFontIcons.iconPanduanti,
          color: Colors.deepOrangeAccent)
    ];
    List<Widget> list = [];
    for (int i = 0; i < listExam.length; i++) {
      SmallSelectedBean smallSelectedBean = listExam.elementAt(i);
      print('----------------------------------------');
      print(smallSelectedBean);
      Widget widget = smallSelecteTestWidget(smallSelectedBean);
      list.add(InkWell(
          onTap: () {
            toPush(smallSelectedBean.title, smallSelectedBean.type,
                smallSelectedBean.url);
          },
          child: widget));
    }

    return list;
  }

  Widget smallSelectedOpenSubject(BuildContext context) {
    final box = GetStorage();
/*    box.write("categoryId", item.id);
    box.write("categoryName", item.name);
    box.write("categoryContent", item.content);*/

    int categoryId = box.hasData("categoryName") ? box.read("categoryId") : 0;
    String categoryName =
        box.hasData("categoryName") ? box.read("categoryName") : "";
    String categoryContent =
        box.hasData("categoryContent") ? box.read("categoryContent") : "";
    return InkWell(
        onTap: () {
          BrnDialogManager.showConfirmDialog(context,
              //  showIcon: true,
              title: S.of(context).qinxuanzecheleixing,
              titleWidget: Container(
                padding: EdgeInsets.only(top: 20, bottom: 10),
                child: MyText(S.of(context).qinxuanzecheleixing),
              ),
              confirm: S.of(context).quedeng,
              cancel: S.of(context).quxiao,
              conformWidget: InkWell(
                  onTap: () {
                    Navigator.pushNamed(context, "/selected");
                    BrnToast.show("确定", context);
                  },
                  child: Center(child: MyText(S.of(context).quedeng))),
              cancelWidget: InkWell(
                  onTap: () {
                    BrnToast.show("取消", context);
                  },
                  child: Center(child: MyText(S.of(context).quxiao))));
        },
        child: Container(
            margin: EdgeInsets.only(left: 10, right: 10, bottom: 30, top: 10),
            padding: EdgeInsets.all(10),
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10)),
              color: Colors.white,
            ),
            height: 50,
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  MyText(categoryName),
                  MyText(categoryContent),
                  Icon(S.current.dir == "rtl"
                      ? IconFontIcons.iconArrowLeft2
                      : IconFontIcons.iconArrowRight2)
                ])));
  }

  void toPushExam(title, type) {
    Map<String, dynamic> params = {
      'subject': subject,
      'type': type,
      "title": title,
    };
    Navigator.pushNamed(context, '/mainexam', arguments: params);
  }

  void toPush(title, type, url) {
    print(url);
    Map<String, dynamic> params = {
      'subject': subject,
      'type': type,
      "title": title,
    };
    Navigator.pushNamed(context, url, arguments: params);
  }
}

class SmallSelectedBean {
  String title;
  IconData icon;
  String url;
  Color color;
  int type;

  SmallSelectedBean(
      {required this.title,
      this.icon = IconFontIcons.iconAShieldDone1,
      this.url = "/mainexam",
      this.color = Colors.amber,
      this.type = 0});

  @override
  String toString() {
    return 'SmallSelectedBean{title: $title, icon: $icon, url: $url, color: $color}';
  }
}
